package com.byit.webservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.byit.builder.MapBuilder;
import com.byit.domain.AppUser;
import com.byit.log.InterLog;
import com.byit.service.IAppUserService;
import com.byit.utils.RedisUtil;
import com.byit.utils.StringUtil;
import com.byit.webservice.service.IWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.Map;

@Component()
@WebService(serviceName = "webService", //与前面的接口一致
        targetNamespace = "http://webservice.byit.com",  //与前面接口一致
        endpointInterface = "com.byit.webservice.service.IWebService")  //接口地址
public class WebServiceImpl implements IWebService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IAppUserService appUserService;

    @Override
    @InterLog(title = "token验证")
    public String isValidToken(String userId, String phone, String token, String comCode, String appCode) {
        Map<String, Object> map = MapBuilder.newMap(2).setParam("code", -1).setParam("msg", "token无效").ok();
        if (getIsValidToken(userId, phone, token)) {
            map.put("code", 1);
            map.put("msg", "token有效");
        }

        return JSON.toJSONString(map);
    }

    @Override
    @InterLog(title = "获取用户信息")
    public String userInfo(String phone, String comCode, String appCode) {
        Map<String, Object> map = MapBuilder.newMap(3).setParam("code", 0).setParam("msg", "无数据").ok();
        AppUser appUser = appUserService.selectByPhone(phone);
        Map<String, Object> user = MapBuilder.newMap(6).ok();
        if (null != appUser) {
            user.put("phone", appUser.getPhone());
            user.put("anonymous", appUser.getAnonymous());
            user.put("userName", appUser.getUserName());
            if (appUser.getAuthState() > 3) {
                user.put("authState", 2);
            } else {
                user.put("authState", appUser.getAuthState());
            }
            user.put("idCard", appUser.getIdCard());
            user.put("sex", appUser.getSex());

            map.put("code", 1);
            map.put("msg", "操作成功");
            map.put("data", user);
        }
        return JSON.toJSONString(map);
    }

    @Override
    @InterLog(title = "获取用户认证信息")
    public String userAuthStauts(String userId, String phone, String token, String comCode, String appCode) {
        return null;
    }


    ////------------------------------------------公共方法------------------------------------------------------

    /**
     * 判断token是否有效
     *
     * @param userId
     * @param phone
     * @param token
     * @return
     */
    private boolean getIsValidToken(String userId, String phone, String token) {
        if (redisUtil.hasKey(IWebService.KEY + userId) && !StringUtil.isEmpty(token)) {
            Object obj = redisUtil.get(IWebService.KEY + userId);
            if (null != obj && token.equals(obj.toString())) {
                return true;
            }
        }
        return false;
    }
}